import Taro, { Component } from '@tarojs/taro'
import { View, RadioGroup, Image, ScrollView, Label, Text, Switch } from '@tarojs/components'
import { connect } from '@tarojs/redux'

import { scroll, unscroll, signed, sign, user, usered } from '../../../actions/counter'
import allExtrance from './../../../config/allExtrance'
import allRequest from './../../../api/allRequest'
import Public from './../../../api/public'
import { SignIn, Tabbar, Footer, GetUserInfo, MoreFormid, DataLoading } from './../../../component/index'
import './my.less'

let enableDebugCounter = 0
let _this

@connect(({ counter }) => ({
    counter
}), (dispatch) => ({
    scroll() {
        dispatch(scroll())
    },
    unscroll() {
        dispatch(unscroll())
    },
    signed() {
        dispatch(signed())
    },
    sign() {
        dispatch(sign())
    },
    usered() {
        dispatch(usered())
    },
    user() {
        dispatch(user())
    },
}))
class My extends Component {

    config = {
        navigationBarTitleText: '我的',
        // navigationBarBackgroundColor:Taro.getStorageSync('baseColor')
    }

    constructor() {
        super()
        Taro.setNavigationBarColor({
            frontColor: Taro.getStorageSync('secondColor'),
            backgroundColor: Taro.getStorageSync('baseColor')
        })
        this.state = {
            isLogined: false,
            myInfo: {},
            signStatus: false,
            isPublic: false,
            changeLoad: true,
            userInfo: {
                avatar_url: ''
            },
            someCount: {}
        }
    }

    componentWillReceiveProps(nextProps) {
        console.log(this.props, nextProps)
        this.setState({ signStatus: this.props.counter && this.props.counter.signStatus })
    }

    componentDidMount() {

    }

    initData() {
        //获取微信信息，缓存获取
        let userInfo = Taro.getStorageSync("user")
        userInfo = JSON.parse(userInfo)
        if (Object.keys(userInfo).length) {
            _this.setState({ userInfo, isLogined: true })
        } else {
            _this.setState({ isLogined: false })
        }

        _this.setState({ userInfo })

        let myInfo = Taro.getStorageSync("worker")
        myInfo = JSON.parse(myInfo)
        _this.setState({ myInfo })

        let shopUser = Taro.getStorageSync("shopUser")
        shopUser = JSON.parse(shopUser)
        if (Object.keys(shopUser).length) {
            allExtrance.util.Taro.setStorageSync('company', shopUser.company_id)
        } else {
            allExtrance.util.Taro.setStorageSync('company', '')
        }

        _this.setState({ user_saas_id: Taro.getStorageSync("user_saas_id") }, () => {
            setTimeout(() => { _this.setState({ changeLoad: false }) }, 500)
        })

        //获取投递收藏信息
        allRequest.personInfo().then((result) => {
            this.setState({ someCount: result })
        }).catch((err) => {
            console.log(err)
        });
    }

    componentWillUnmount() { }

    componentDidShow() {
        _this = this
        if (Taro.getEnv() === Taro.ENV_TYPE.WEB) {
            Public.Person().then((result) => {
                this.initData()
            })
        } else {
            this.initData()
        }
    }

    componentDidHide() { }

    refresh() {
        _this.initData()
    }

    // toLogin(isLogined) {
    //     if (isLogined) {
    //         return
    //     }
    //     login('worker')
    //         .then(res => {
    //         })
    //         .catch(err => { console.log(err) })
    // }

    enableDebug(e) {
        let { isLogined } = this.state
        if (isLogined) {
            if (Taro.getStorageSync("hasWechatUser")) {
                if (++enableDebugCounter > 10) {
                    Taro.clearStorageSync()
                    if (Taro.getEnv() === Taro.ENV_TYPE.WEAPP) {
                        Taro.setEnableDebug({
                            enableDebug: true
                        })
                    }
                    Public.Person().then((result) => {
                        this.initData()
                    })
                }
            } else {
                this.props.usered()
            }
        } else {
            this.props.sign()
        }
    }

    toCompany(e) {
        if (this.state.isLogined) {
            if (Taro.getStorageSync("hasWechatUser")) {
                let shopUser = Taro.getStorageSync("shopUser")
                shopUser = JSON.parse(shopUser)
                let company = Object.keys(shopUser).length
                if (company) {
                    Taro.redirectTo({
                        url: '/pages/business/index/index'
                    })
                } else {
                    Taro.navigateTo({
                        url: '/subPackages/subPackageF/myShop/myShop?new=true'
                    })
                }
            } else {
                this.props.usered()
            }

        } else {
            this.props.sign()
        }
    }

    toNavigator(url, e) {
        let { isLogined } = this.state
        if (isLogined) {
            if (Taro.getStorageSync("hasWechatUser")) {
                Taro.navigateTo({
                    url: url
                })
            } else {
                this.props.usered()
            }
        } else {
            this.props.sign()
        }
    }

    hasWechatUser(e) {
        _this.props.user()
        Public.Person().then((result) => {
            _this.initData()
        })
    }

    showLogin() {
        let { isLogined } = _this.state
        if (isLogined) {
            if (Taro.getStorageSync("hasWechatUser")) {

            } else {
                _this.props.usered()
            }
        } else {
            _this.props.sign()
        }
    }

    controlPublic(type, e) {
        // e.stopPropagation()
        this.setState({
            isPublic: type
        })
    }

    changePublic(e) {
        e.stopPropagation()
        let { myInfo } = this.state
        myInfo.is_open = e.detail.value
        this.setState({
            myInfo,
            isPublic: false
        }, () => {
            if(myInfo.is_open){
                allExtrance.util.Taro.showToast('简历公开成功')
            }else{
                allExtrance.util.Taro.showToast('简历隐藏成功')
            }
        })
        allExtrance.util.Taro.setStorageSync('worker', myInfo)
        allRequest.workerStatus({ status: e.detail.value })


    }

    stopPropagation(e) {
        e.stopPropagation()
    }

    render() {
        const currentPage = 'consumerMy'
        let { isLogined, myInfo, signStatus, user_saas_id, userInfo, changeLoad, isPublic, someCount } = this.state
        return (
            <View className='consumerMy'>
                {/* {Taro.getEnv() === Taro.ENV_TYPE.WEB && <TabTop></TabTop>} */}
                <ScrollView className='myContent' scrollY onScrollToUpper={this.refresh} style={`${this.props.counter && (this.props.counter.scroll ? 'height:100vh;overflow:hidden' : '')}`}>
                    <View className='content'>
                        {/* <Btn type='wrapper'> */}
                        {/* <View className='header' style={`background-color:${Taro.getStorageSync('baseColor')}}>
                            {
                                isLogined ?
                                    <View className='person-info'>
                                        <Image mode='aspectFill' className='avatar' src={`${myInfo.worker_avatar || userInfo.avatar_url || 'https://file.open.banchengyun.com/2019/04/01/155411354356696.png'}`} onClick={this.enableDebug.bind(this)} />
                                        {(!!myInfo.worker_nickname || userInfo.nick_name) && <View className='name'>{myInfo.worker_nickname || userInfo.nick_name}</View>}
                                        {(!!Taro.getStorageSync('phone')) && <View className='phone'>{Taro.getStorageSync('phone')}</View>}
                                    </View>
                                    :
                                    <View className='person-info'>
                                        <Image className='avatar' src='https://file.open.banchengyun.com/2019/04/01/155411354356696.png' onClick={this.enableDebug.bind(this)} />
                                        <View className='name' onClick={this.showLogin}>未登录</View>
                                        <View className='login' onClick={this.showLogin}>
                                            点击一键登陆
                                            <View className='right-icon'></View>
                                        </View>
                                    </View>
                            }

                            <Image className='wave' src='https://file.open.banchengyun.com/2019/04/01/155411357226648.png' />
                        </View> */}
                        <View className='headerBack' style={`background-color:${Taro.getStorageSync('baseColor')}`}></View>
                        <View className='headerTwo'>
                            <View className='headerRowOne'>
                                <View className='headImg'>
                                    <Image className='headAvator' mode='aspectFill' src={`${myInfo.worker_avatar || userInfo.avatar_url || 'https://file.open.banchengyun.com/2019/04/01/155411354356696.png'}`} onClick={this.enableDebug.bind(this)} ></Image>
                                </View>
                                {
                                    isLogined
                                        ? <View className='headCon'>
                                            <View className='username'>{myInfo.worker_nickname || userInfo.nick_name}</View>
                                            <Text className='userCon'>ID:    {Taro.getStorageSync('phone') || userInfo.phone}</Text>
                                        </View>
                                        : <View className='headCon'>
                                            <View className='username' onClick={this.showLogin}>未登录</View>
                                            <View className='userCon' onClick={this.showLogin}>点击一键登录</View>
                                        </View>
                                }
                            </View>
                            <View className='headerRowTwo'>
                                <View className='numConBlock'>
                                    <View className='conItem' onClick={this.toNavigator.bind(this, '/subPackages/subPackageS/jobList/jobList?type=post')}>
                                        <View className='itemTitle'>投递记录</View>
                                        <View className='itemNum'>{someCount.workerApplyCount}</View>
                                    </View>
                                    <View className='conItem' onClick={this.toNavigator.bind(this, '/subPackages/subPackageS/jobList/jobList?type=collect')}>
                                        <View className='itemTitle'>收藏职位</View>
                                        <View className='itemNum'>{someCount.jobStarCount}</View>
                                    </View>
                                    {/* <View className='conItem'>
                                        <View className='itemTitle'>关注公司</View>
                                        <View className='itemNum'>99</View>
                                    </View> */}
                                </View>
                            </View>
                        </View>
                        <View className='cell' style='margin-top:0'>
                            <MoreFormid>
                                <View className='row' onClick={this.toNavigator.bind(this, '/subPackages/subPackageS/myResume/myResume')}>
                                    <View className='label'>
                                        <View className='colorBlock' style={`${'background:' + Taro.getStorageSync('baseColor')}`}>
                                            <Image className='icon icon-my-resume' src='https://file.open.banchengyun.com/353/51b4af395f56339ba2067aec532db707' />
                                        </View>
                                        <View className='name'>我的简历</View>
                                    </View>
                                    <View className='right'>
                                        {myInfo.complete ? <View className='text'>简历完整度 {myInfo.complete}</View> : <View className='text not-login' style={`background-color:${Taro.getStorageSync('baseColor')}`}>创建简历，快速找到工作</View>}
                                        <View className='right-icon'></View>
                                    </View>
                                </View>
                            </MoreFormid>
                            {/* <MoreFormid>
                                <View className='row' onClick={this.toNavigator.bind(this, '/subPackages/subPackageS/myResume/myResume')}>
                                    <View className='label'>
                                        <View className='colorBlock' style={`${'background:' + Taro.getStorageSync('baseColor')}`}>
                                            <Image className='icon icon-my-resume' src='https://file.open.banchengyun.com/353/c55f9a6dfd37463b4695fc4f2fe026e3' />
                                        </View>
                                        <View className='name'>求职意向</View>
                                    </View>
                                    <View className='right'>
                                        <View className='right-icon'></View>
                                    </View>
                                </View>
                            </MoreFormid> */}
                            {
                                myInfo.complete  && <MoreFormid>
                                    <View className='row' >
                                        <View className='label'>
                                            <View className='colorBlock' style={`${'background:' + Taro.getStorageSync('baseColor')}`}>
                                                <Image className='icon icon-my-post' src='https://file.open.banchengyun.com/353/447308b6ca4e5bd84ecc67016aca1b40' />
                                            </View>
                                            <View className='name'>公开设置</View>
                                        </View>
                                        <View className='right'>
                                            {/* <View className='text'>{myInfo.is_open == 1 ? '公开' : '隐身'}</View>
                                            <View className='right-icon'></View> */}
                                            {/* onClick={this.controlPublic.bind(this, true)} */}
                                            <Switch onChange={this.changePublic.bind(this)} checked={myInfo.is_open} style='margin-right:-10px' color={Taro.getStorageSync('baseColor')} />
                                        </View>
                                    </View>
                                </MoreFormid>
                            }
                        </View>
                        <MoreFormid>
                            <View className='cell'>
                                <View className='row' hover-class='none' onClick={this.toNavigator.bind(this, '/subPackages/subPackageS/rpInfo/rpInfo')}>
                                    <View className='label'>
                                        <View className='colorBlock' style={`${'background:' + Taro.getStorageSync('baseColor')}`}>
                                            <Image className='icon icon-my-wallet' src='https://file.open.banchengyun.com/353/2a7f5f8db80a797c4ff1c4f43221a4db' />
                                        </View>
                                        <View className='name'>我的钱包</View>
                                    </View>
                                    <View className='right-icon'></View>
                                </View>
                                {/* <View className='row' hover-class='none' onClick={this.toNavigator.bind(this, '/subPackages/subPackageS/setting/setting')}>
                                    <View className='label'>
                                        <View className='colorBlock' style={`${'background:' + Taro.getStorageSync('baseColor')}`}>
                                            <Image className='icon icon-my-wallet' src='https://file.open.banchengyun.com/353/78872a9a81ce1375ae9fcf27d88385fa' />
                                        </View>
                                        <View className='name'>设置</View>
                                    </View>
                                    <View className='right-icon'></View>
                                </View> */}
                            </View>
                        </MoreFormid>
                        <MoreFormid>
                            <View className='cell'>
                                <View className='row' hover-class='none' onClick={this.toCompany.bind(this)}>
                                    <View className='label'>
                                        <View className='colorBlock' style={`${'background:' + Taro.getStorageSync('baseColor')}`}>
                                            <Image className='icon icon-my-wallet' src='https://file.open.banchengyun.com/353/4e7cce6537865d89ed62b4b9a9d9d3a8' />
                                        </View>
                                        <View className='name'>切换至我要招人</View>
                                    </View>
                                    <View className='right-icon'></View>
                                </View>
                            </View>
                        </MoreFormid>
                        <Footer></Footer>
                    </View>
                </ScrollView>
                {
                    // isPublic && <View className='zhezhaoPublic' onClick={this.controlPublic.bind(this, false)}>
                    //     <View className='resumeSetting'>
                    //         <RadioGroup style='height:150px;' onChange={this.changePublic.bind(this)}>
                    //             <View className='radioList'>
                    //                 <Label className='body_title'>简历公开状态</Label>
                    //                 <Label className='body_label'>
                    //                     简历隐身
                    //                 <Radio value='0' checked={`${myInfo.is_open == 0 ? 'true' : ''}`}></Radio>
                    //                 </Label>
                    //                 <Label className='body_label'>
                    //                     简历公开
                    //                 <Radio value='1' checked={`${myInfo.is_open == 1 ? 'true' : ''}`}></Radio>
                    //                 </Label>
                    //             </View>
                    //         </RadioGroup>
                    //     </View>
                    // </View>
                }
                <Tabbar currentPage={currentPage}></Tabbar>
                {this.props.counter && (this.props.counter.signStatus && <SignIn onRefresh={this.initData}></SignIn>)}
                {changeLoad && <DataLoading></DataLoading>}
                {this.props.counter && this.props.counter.hasUser && <GetUserInfo onHasWechatUser={this.hasWechatUser} url='/pages/consumer/my/my'></GetUserInfo>}
            </View>

        )
    }
}

export default My
